package org.hitchhike.web.front.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.hitchhike.common.domain.BaseEntity;
import org.hitchhike.common.web.controller.BaseController;
import org.hitchhike.core.domain.Driver;
import org.hitchhike.core.domain.Passenger;
import org.hitchhike.core.domain.User;
import org.hitchhike.core.enums.ImageTypeEnum;
import org.hitchhike.core.exception.JsonException;
import org.hitchhike.core.form.VerForm;
import org.hitchhike.core.service.UserService;
import org.hitchhike.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping(value = "/front")
public class VerController extends BaseController<BaseEntity, Long> {

  @Autowired
  private UserService userService;
  @Autowired
  private FileUtil fileUtil;

  @RequestMapping(value = "/ver", method = RequestMethod.GET)
  public String ver(Model model) {
    User user = userService
        .findByUsername(SecurityContextHolder.getContext().getAuthentication().getName());
    model.addAttribute("user", user);
    model.addAttribute("ver", "active");
    return viewName("ver");
  }

  @RequestMapping(value = "/ver/save", method = RequestMethod.POST)
  public String save(VerForm verForm, HttpServletRequest request) {
    User user = userService
        .findByUsername(SecurityContextHolder.getContext().getAuthentication().getName());

    Passenger passenger = user.getPassengerInfo();
    passenger.setIdCard(verForm.getIdCard());

    Driver driver = user.getDriverInfo();
    driver.setDrivingLicenseNumber(verForm.getDrivingLicenseNumber());
    driver.setLicencePlateNumber(verForm.getLicencePlateNumber());
    driver.setTravelLicenseDate(verForm.getTravelLicenseDate());

    user.setPassenger(passenger);
    user.setDriver(driver);
    try {
      userService.update(user);
    } catch (Exception e) {
      throw new JsonException(e.getMessage());
    }

    return redirectToUrl(viewName("ver"));
  }

  @ResponseBody
  @RequestMapping("/ver/upload")
  public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request,
      String imageType) {
    User user = userService
        .findByUsername(SecurityContextHolder.getContext().getAuthentication().getName());

    String prefix = fileUtil.getPrefix(file.getOriginalFilename()).replace(".", "@");
    String fileName = user.getUsername() + "_";
    try {

      if (imageType.equals("sfz_a") || imageType.equals("sfz_b")) {
        Passenger passenger = user.getPassengerInfo();
        if (imageType.equals("sfz_a")) {
          fileName += ImageTypeEnum.IDCARD_FRONT_PHOTO + prefix;
          passenger.setPhotoFront(fileName);
        } else if (imageType.equals("sfz_b")) {
          fileName += ImageTypeEnum.IDCARD_BACK_PHOTO + prefix;
          passenger.setPhotoBack(fileName);
        }
        user.setPassenger(passenger);
      }

      if (imageType.equals("xsz") || imageType.equals("jsz")) {
        Driver driver = user.getDriverInfo();
        // 行驶证
        if (imageType.equals("xsz")) {
          fileName += ImageTypeEnum.TRAVEL_LICENSE_PHOTO + prefix;
          driver.setTravelLicensePhoto(fileName);
        }
        // 驾驶证
        else if (imageType.equals("jsz")) {
          fileName += ImageTypeEnum.DRIVING_LICENSE_PHOTO + prefix;
          driver.setDrivingLicensePhoto(fileName);
        }
        user.setDriver(driver);
      }
      userService.update(user);

      fileUtil.upload(request, file, fileName.replace("@", "."));
    } catch (IllegalStateException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return "1";
  }

}
